/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hvtt.web.utilities;

import com.google.gson.Gson;
import org.json.simple.JSONValue;

/**
 *
 * @author Administrator
 */
public class SocialAPIWrapper {

    private static String appId = "748155828562101";
    private static String appSecret = "2ffa9772c8491b05c25e3f08cf8ba14f";
    private static String redirectUrl = Utilities.getRequest().getScheme() + "://"
            + Utilities.getRequest().getServerName()
            + ":" + Utilities.getRequest().getServerPort() + "/facebookCallBack.ht";
    private String accessToken;
    private Gson gson;

    public SocialAPIWrapper() {
        gson = new Gson();
    }

    public static String getDialogLink() {
        String url = "https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=%s";
        return String.format(url, appId, redirectUrl);
    }

    public String getAccessToken(String code) {
        String accessTokenLink = "https://graph.facebook.com/oauth/access_token?"
                + "client_id=%s&client_secret=%s&redirect_uri=%s&code=%s";
        accessTokenLink = String.format(accessTokenLink, appId, appSecret, redirectUrl, code);
        String result = Utilities.getResult(accessTokenLink);
        String token = result.substring(result.indexOf("=") + 1, result.indexOf("&"));
        return token;
    }

    public Object getUserInfo() {
        String infoUrl = "https://graph.facebook.com/me?access_token=%s&scope=email,publish_stream,public_profile,user_birthday,user_location,user_about_me,user_likes";
        infoUrl = String.format(infoUrl, this.accessToken);
        String result = Utilities.getResult(infoUrl);
        return JSONValue.parse(result);
    }

    /**
     * @return the accessToken
     */
    public String getAccessToken() {
        return accessToken;
    }

    /**
     * @param accessToken the accessToken to set
     */
    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    /**
     * @return the gson
     */
    public Gson getGson() {
        return gson;
    }

    /**
     * @param gson the gson to set
     */
    public void setGson(Gson gson) {
        this.gson = gson;
    }
}
